import { authHeader, host } from './server';



export const listService = {
    getList,
    getOne,

};

function getList(url,refresh) {
    /*
    if(refresh==null){
   let data = JSON.parse(sessionStorage.getItem(url));
  if (data && data.length>0){return Promise.resolve(data);}
    }
    */
    const requestOptions = {
        method: 'GET',
        headers: authHeader(),
    };
    
    return fetch(host + url, requestOptions)
        .then(handleResponse);
}


function getOne(url,refresh) {
  
/*     if(!refresh){
        let data = JSON.parse(sessionStorage.getItem(url));
        if (data){return Promise.resolve(data);}
    } */
    const requestOptions = {
        method: 'GET',
        headers: authHeader(),
    };

    return fetch(host + url, requestOptions)
        .then(handleResponse).then(
            data=>
            {
             //   sessionStorage.setItem(url, JSON.stringify(data))
                return data},
            e=>e
        );
}

function handleResponse(response) {
    if (!response.ok) { 
        
        return Promise.reject(response.json());
    }
    if(response.status===204){return Promise.resolve(response.statusText); }
    return response.json();
}